﻿//=======================================================
// Author: Jonas Follesø <jonas@follesoe.no>
// Copyright (c) 2008
//
// Licensed under the Microsoft Public License (Ms-PL)
// A copy of the license is available at 
// http://www.opensource.org/licenses/ms-pl.html
//=======================================================

using Microsoft.Expression.Framework;
using Microsoft.Expression.Framework.Commands;

using Colorful.Control;
using Colorful.Control.Services;

namespace Colorful.Blend.AddIn
{
    public class ColorCommunityAddIn : CommandTarget, IPackage
    {
        private IApplicationService appService;
        private SearchPanel colorPanel;
        private ColorfulViewModel viewModel;

        public void Initialize()
        {
            this.viewModel = new ColorfulViewModel( new AdobeKuler(), new CfXamlClipboardFormatter(), new XamlTextClipboardFormatter() );
            this.viewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler( OnViewModelPropertyChanged );
            this.colorPanel = new SearchPanel( viewModel, true );

            appService.CommandService.AddTarget( this );

            if (appService.WindowService.PaletteRegistry["ColorPanel"] == null)
                appService.WindowService.RegisterPalette( "ColorPanel", colorPanel, "Colorful" );
        }

        private void OnViewModelPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ErrorMessage" && viewModel.ErrorMessage != string.Empty)
            {
                appService.MessageLoggingService.WriteLine("An Exception occurred in the Colorful Expression AddIn");
                appService.MessageLoggingService.WriteLine(viewModel.ErrorMessage);
            }
        }

        public void Load(IApplicationService applicationService)
        {
            appService = applicationService;
            Initialize();
        }

        public void Unload()
        {

        }
    }
}